{% extends 'layout/base.html' %}

{% block css %}
    <style>
        .error-msg {
            color: red;
        }
    </style>

{% endblock %}


{% block content %}
    <div class="container-fluid">
        <div class="card">
            <div class="card-header" style="display: flex; justify-content: space-between;">
                <div>
                    <a href="{% url 'domain' %}">
                        {% if file_obj %}
                            <i class="fa fa-home" aria-hidden="true"></i>
                            <span>{{ file_obj.it_name }}</span>
                        {% endif %}
                    </a>
                </div>
                <div class="function">
                    <a class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#addModal"
                       data-whatever="新建域名"><i class="fa fa-plus-circle" aria-hidden="true"></i> 新增域名
                    </a>
                </div>
            </div>
            <div class="card-body">
                <table class="table">
                    <thead>
                    <tr>
                        <th>网站类型</th>
                        <th>域名</th>
                        <th>环境</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="rowList">
                    {% for item in domain_obj %}
                        <tr>
                            <td>{{ item.get_site_type_display }}</td>
                            <td>{{ item.url_name }}</td>
                            <td>{{ item.get_context_display }}</td>
                            <td>{{ item.comment }}</td>
                            <td>
                                <a class="btn btn-default btn-sm"
                                   data-bs-toggle="modal"
                                   data-bs-target="#addModal"
                                   data-url_name="{{ item.url_name }}"
                                   data-context="{{ item.context }}"
                                   data-site_type="{{ item.site_type }}"
                                   data-comment="{{ item.comment }}"
                                   data-fid="{{ item.id }}"
                                   data-whatever="修改域名"><i class="fa fa-pencil-square-o" aria-hidden="true" title="编辑"></i></a>
                                <a class="btn btn-danger btn-sm"
                                   data-bs-toggle="modal"
                                   data-bs-target="#myModal"
                                   data-fid="{{ item.id }}"><i class="fa fa-trash" aria-hidden="true" title="删除"></i></a></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!--添加和编辑modal对话框-->
    <div class="modal fade" id="addModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
         aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="staticBackdropLabel"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="addForm">
                        {% csrf_token %}
                        <input type="hidden" name="fid" id="fid">
                        {% for field in form %}
                            <div class="form-group">
                                {% if field.name == "comment" %}
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg"></span>
                                {% else %}
                                    <span style="color: red">*</span>
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg"></span>
                                {% endif %}

                            </div>
                        {% endfor %}
                        <br/>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="bindSubmit">保存</button>
                </div>
            </div>
        </div>
    </div>
    <!--删除modal对话框-->
    <div class="modal fade" id="myModal">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h5 class="modal-title">提示</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <!-- 模态框内容 -->
                <div class="modal-body">
                    <h6>确定删除?</h6>
                </div>
                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" id="btnDelete" class="btn btn-danger" data-bs-dismiss="modal">确定
                    </button>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script>
        var FILE_DELETE_URL = "{% url 'domain_delete' %}";
        $(function () {
            initAddModal();
            bindModelSubmit();
            bindDeleteSubmit();
        });

        function initAddModal() {
            $("#addModal").on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                var recipient = button.data('whatever');
                var fid = button.data('fid');
                var url = button.data('url_name');
                var evn = button.data('context');
                var comment = button.data('comment');
                var site_type = button.data('site_type');
                var modal = $(this);
                modal.find('.modal-title').text(recipient);
                if (fid) {
                    modal.find('.error-msg').empty();
                    modal.find('#id_url_name').val(url);
                    modal.find('#id_context').val(evn);
                    modal.find('#id_site_type').val(site_type);
                    modal.find('#id_comment').val(comment);
                    modal.find('#fid').val(fid);
                } else {
                    modal.find('.error-msg').empty();
                    $('#addForm')[0].reset();
                }
            });
            /*删除modal对话框*/
            $("#myModal").on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                var fid = button.data('fid');
                $('#btnDelete').attr('fid', fid);
            })
        }

        /*新建文件夹*/
        function bindModelSubmit() {
            $("#bindSubmit").click(function () {
                $.ajax({
                    url: location.href,
                    type: "POST",
                    data: $("#addForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            window.location.href = location.href
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }

                })
            })
        }


        /*删除域名modal对话框*/
        function bindDeleteSubmit () {
            $("#btnDelete").click(function () {
                $.ajax({
                    url:FILE_DELETE_URL,
                    type: "GET",
                    data: {fid: $(this).attr('fid')},
                    success:function (res) {
                        if (res.status){
                            window.location.href = location.href;
                        }

                    }
                })
            })

        }


    </script>


{% endblock %}